//
//  EditPersonViewController.m
//  bluetooth
//
//  Created by 周季阳 on 15/7/15.
//  Copyright (c) 2015年 switch. All rights reserved.
//

#import "EditPersonViewController.h"
#import "LoginViewController.h"
#import "AppDelegate.h"
#import "UIViewAdditions.h"
@interface EditPersonViewController ()<UITextFieldDelegate,UITextViewDelegate,UIPickerViewDataSource,UIPickerViewDelegate>
{
    UITextField *nameTextField;
    UITextField *phoneTextField;
    UITextView *addressTextField;
    UIPickerView *_picker;
    UIButton *zPickerBtn;
    NSInteger _select;//选择
}
@property(nonatomic,retain)NSString *zPID;
@property(nonatomic,retain)NSString *zCID;
@property(nonatomic,retain)NSMutableArray  *zArrayData;
@property(nonatomic,assign)NSInteger zIndex;
@property (nonatomic,assign) NSInteger zProvinceIndex;
@property (nonatomic,assign) NSInteger zCityIndex;
@property (nonatomic,assign) NSInteger zZhengJianIndex;

@end
#define Province 0 //省
#define City 1  //市
#define Qu 2   //区
@implementation EditPersonViewController
@synthesize zArrayData,zPID,zIndex,zCID,zCityIndex,zProvinceIndex,zZhengJianIndex;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initNav];
    self.view.backgroundColor = [self colorWithHexString:@"#F5F5F5"];
    
    navTitle.text = @"个人中心";
    [navRightBtn setTitle:@"更换账号" forState:UIControlStateNormal];
    navRightBtn.frame = CGRectMake(SCREEN_WIDTH-74 , 20, 64, SCREEN_WIDTH*0.2-20);
    navRightBtn.hidden = NO;
    
    [navBtn setImage:[UIImage imageNamed:@"制作完成_03"] forState:UIControlStateNormal];
    navBtn.frame = CGRectMake(10, 30, 63, 21);
    
    [self initUI];
    zPID = @"0";
    zCID = @"0";
    zProvinceIndex = 0;
    zCityIndex = 0;
    zZhengJianIndex = 0;
}
//当用户按下return键或者按回车键，keyboard消失
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
#pragma mark - initUI
-(void)initUI{
    UIImageView *nameImg = [[UIImageView alloc]initWithFrame:CGRectMake(10, navView.bottom+10, SCREEN_WIDTH-20, 25)];
    nameImg.userInteractionEnabled = YES;
    [nameImg setImage:[UIImage imageNamed:@"个人中心---编辑_03"]];
    [self.view addSubview:nameImg];
    
    nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0 ,SCREEN_WIDTH-40 , 25)];
    nameTextField.placeholder = @"姓名";
    nameTextField.font = [UIFont systemFontOfSize:14];
    nameTextField.backgroundColor= [UIColor clearColor];
    [nameImg addSubview:nameTextField];
    nameTextField.delegate = self;

    UIImageView *phoneImg = [[UIImageView alloc]initWithFrame:CGRectMake(10, nameImg.bottom+10, SCREEN_WIDTH-20, 25)];
    phoneImg.userInteractionEnabled = YES;
    [phoneImg setImage:[UIImage imageNamed:@"个人中心---编辑_03"]];
    [self.view addSubview:phoneImg];
    
    phoneTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0 ,SCREEN_WIDTH-40 , 25)];
    phoneTextField.placeholder = @"电话（您的账号）";
    phoneTextField.font = [UIFont systemFontOfSize:14];
    phoneTextField.backgroundColor= [UIColor clearColor];
    [phoneImg addSubview:phoneTextField];
    phoneTextField.delegate = self;

    UIImageView *provinceImg = [[UIImageView alloc]initWithFrame:CGRectMake(10, phoneImg.bottom+10, 140, 25)];
    [provinceImg setImage:[UIImage imageNamed:@"个人中心---编辑_06"]];
    provinceImg.userInteractionEnabled = YES;
    [self.view addSubview:provinceImg];
    
    UIButton *provinceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    provinceBtn.frame = CGRectMake(10, 0, provinceImg.frame.size.width-20, 25);
    [provinceBtn setTitle:@"请选择省份" forState:UIControlStateNormal];
    provinceBtn.tag = 10;
    [provinceBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [provinceBtn addTarget:self action:@selector(clickProvinceBtn:) forControlEvents:UIControlEventTouchUpInside];
    [provinceImg addSubview:provinceBtn];
    provinceBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    provinceBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    
    UIImageView *cityImg = [[UIImageView alloc]initWithFrame:CGRectMake(provinceImg.right+20, phoneImg.bottom+10, 140, 25)];
    [cityImg setImage:[UIImage imageNamed:@"个人中心---编辑_06"]];
    cityImg.userInteractionEnabled = YES;
    [self.view addSubview:cityImg];
    
    UIButton *cityBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cityBtn.frame = CGRectMake(10, 0, cityImg.frame.size.width-20, 25);
    [cityBtn setTitle:@"选择市" forState:UIControlStateNormal];
    [cityBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [cityBtn addTarget:self action:@selector(clickProvinceBtn:) forControlEvents:UIControlEventTouchUpInside];
    [cityImg addSubview:cityBtn];
    cityBtn.tag = 11;
    cityBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    cityBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    
    UIImageView *countryImg = [[UIImageView alloc]initWithFrame:CGRectMake(10, cityImg.bottom+10, 300, 25)];
    [countryImg setImage:[UIImage imageNamed:@"个人中心---编辑_09"]];
    countryImg.userInteractionEnabled = YES;
    [self.view addSubview:countryImg];

    UIButton *countryBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    countryBtn.frame = CGRectMake(10, 0, countryImg.frame.size.width-20, 25);
    [countryBtn setTitle:@"请选择区县" forState:UIControlStateNormal];
    [countryBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [countryBtn addTarget:self action:@selector(clickProvinceBtn:) forControlEvents:UIControlEventTouchUpInside];
    [countryImg addSubview:countryBtn];
    countryBtn.tag = 12;
    countryBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    countryBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    
    UIImageView *addressImg = [[UIImageView alloc]initWithFrame:CGRectMake(10, countryImg.bottom+10, SCREEN_WIDTH-20, 64)];
    addressImg.userInteractionEnabled = YES;
    [addressImg setImage:[UIImage imageNamed:@"个人中心---编辑_12"]];
    [self.view addSubview:addressImg];
    
    addressTextField = [[UITextView alloc] initWithFrame:CGRectMake(5, 0 ,addressImg.frame.size.width-10 , 64)];
    addressTextField.text = @"详细地址";
    addressTextField.textColor = [UIColor lightGrayColor];
    addressTextField.font = [UIFont systemFontOfSize:14];
    addressTextField.backgroundColor= [UIColor clearColor];
    [addressImg addSubview:addressTextField];
    addressTextField.delegate = self;
    
    for (int i = 0; i < 2; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(10+160*i, addressImg.bottom+10, 140, 35);
        btn.tag = i;
        if (i==0) {
            [btn setBackgroundImage:[UIImage imageNamed:@"个人中心---编辑_15"] forState:UIControlStateNormal];
        }else{
            [btn setBackgroundImage:[UIImage imageNamed:@"个人中心---编辑_17"] forState:UIControlStateNormal];
        }
        [btn addTarget:self action:@selector(clickSaveBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
    }

}
#pragma mark - 点击保存|取消
-(void)clickSaveBtn:(UIButton *)sender{
    if (sender.tag == 0) {
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}
#pragma mark - 点击省市区
-(void)clickProvinceBtn:(UIButton *)sender{
    if (sender.tag == 10){
    if (_picker) {
//        YoungAllData *data = [[YoungAllData alloc]init];
//        data.delegate=self;
//        [data gainBackProvince];
        zArrayData = [[NSMutableArray alloc]initWithObjects:@"北京", nil];;
        [_picker reloadAllComponents];
        zPickerBtn.hidden = NO;
        _picker.hidden = NO;
        
    }else
        //开户行所在的省
    {
        zArrayData = [[NSMutableArray alloc]initWithObjects:@"北京", nil];;
        
//        YoungAllData *data = [[YoungAllData alloc]init];
//        data.delegate=self;
//        [data gainBackProvince];
        
        
        zIndex = 0;
        
        //创建省市区选择框
        _picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 180, SCREEN_WIDTH, 200)];
        _picker.backgroundColor = LineColor;
        _picker.layer.borderColor = BackColor.CGColor;
        _picker.layer.borderWidth = 1.0;
        _picker.dataSource = self;
        _picker.delegate = self;
        _picker.hidden = NO;
        _picker.showsSelectionIndicator = NO;
        [self.view addSubview:_picker];
        
        if (zPickerBtn) {
            zPickerBtn.hidden = NO;
        }else{
            zPickerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [zPickerBtn setBackgroundColor:[UIColor blackColor]];
            zPickerBtn.alpha = 0.4;
            zPickerBtn.frame = CGRectMake(0, SCREEN_HEIGHT - 103, SCREEN_WIDTH, 26);
            [zPickerBtn addTarget:self action:@selector(clickBtn) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview:zPickerBtn];
        }
        _select = 0;
        
//        UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap1)];
//        [singleTapGestureRecognizer setNumberOfTapsRequired:1];
//        [self.view addGestureRecognizer:singleTapGestureRecognizer];
        
        
    }
    }else if (sender.tag == 11){
        if ([zPID isEqualToString:@"0"]) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"请先选择省" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }else{
            zCityIndex = 0;
            _picker.hidden = NO;

            zArrayData = [[NSMutableArray alloc]initWithObjects:@"北京市", nil];;
            [_picker reloadAllComponents];
            zPickerBtn.hidden = NO;
            _select = 1;
        }

    }else if (sender.tag == 12){
        if ([zPID isEqualToString:@"0"]) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"请先选择省" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }else if([zCID isEqualToString:@"0"]){
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"请先选择市" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }else{
            
            zArrayData = [[NSMutableArray alloc]initWithObjects:@"朝阳区", nil];;
            _picker.hidden = NO;

            [_picker reloadAllComponents];
            zPickerBtn.hidden = NO;
            
            _select = 2;
        }

    }
}

#pragma mark - UIpickerView 选择Btn
-(void)clickBtn{
    self.tabBarController.tabBar.hidden = NO;
    
    UIButton *btn = (UIButton *)[self.view viewWithTag:10];
    UIButton *btn1 = (UIButton *)[self.view viewWithTag:11];
    UIButton *btn2 = (UIButton *)[self.view viewWithTag:12];
    switch (_select) {
        case Province://选择省
            //[_addressView.provinceBut setTitle:pickerArray[row] forState:UIControlStateNormal];
            zProvinceIndex = zIndex;
            [btn setTitle:[zArrayData objectAtIndex:0] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            //zPID =[NSString stringWithFormat:@"%@",[[zArrayData objectAtIndex:zIndex] objectForKey:@"BankProvinceID"]];
            _picker.hidden = YES;
            zPID = @"1";
            [btn1 setTitle:@"选择市" forState:UIControlStateNormal];
            [btn1 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            zCID = @"0";
            break;
            
        case City://市
            //[_addressView.cityBut setTitle:pickerArray[row] forState:UIControlStateNormal];
            [btn1 setTitle:[zArrayData objectAtIndex:0] forState:UIControlStateNormal];
            [btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            zCID = @"1";
            //zCID = [NSString stringWithFormat:@"%@",[[zArrayData objectAtIndex:zCityIndex] objectForKey:@"BankCityID"]];
            _picker.hidden = YES;
            
            
            break;
        case Qu://市
            //[_addressView.cityBut setTitle:pickerArray[row] forState:UIControlStateNormal];
            [btn2 setTitle:[zArrayData objectAtIndex:0] forState:UIControlStateNormal];
            [btn2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            
            _picker.hidden = YES;
            
            
            break;
            
    }
    zPickerBtn.hidden = YES;
}
#pragma -mark UIPickerView 的代理

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return [zArrayData count];
}
-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    UIButton *btn = (UIButton *)[self.view viewWithTag:10];
    UIButton *btn1 = (UIButton *)[self.view viewWithTag:11];
    UIButton *btn2 = (UIButton *)[self.view viewWithTag:12];
    if (_select == 0) {
        if (row == 0) {
            [btn setTitle:[zArrayData objectAtIndex:0] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            //zPID =[NSString stringWithFormat:@"%@",[[zArrayData objectAtIndex:zProvinceIndex] objectForKey:@"BankProvinceID"]];
            zPID = @"1";
        }
        return [zArrayData objectAtIndex:0];
        
    }else if (_select == 1){
        if (row == 0) {
            [btn1 setTitle:[zArrayData objectAtIndex:0] forState:UIControlStateNormal];
            [btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            //zCID = [NSString stringWithFormat:@"%@",[[zArrayData objectAtIndex:zCityIndex] objectForKey:@"BankCityID"]];
            zCID = @"1";
        }
        return [zArrayData objectAtIndex:0];
        
    }else{
        if (row == 0) {
            [btn2 setTitle:[zArrayData objectAtIndex:0] forState:UIControlStateNormal];
            [btn2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
        return [zArrayData objectAtIndex:0];
        
    }
}

#pragma mark - 更换账号
-(void)clickRightBtn{
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    // 在delegate中初始化新的controller
    //[delegate initTabbarController];
    // 修改rootViewController
    LoginViewController *mainVC = [LoginViewController new];
    UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:mainVC];
    
    [delegate.window addSubview:nav1.view];
    [self.view removeFromSuperview];
    delegate.window.rootViewController = nav1;
    
}
#pragma mark - 返回菜单
-(void)clickBackBtn{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
